<?php
	defined('IN_TS') or die('Access Denied.');
	
	$userid = aac('user')->isLogin();
	
	
	switch($ts){
		//基本信息
		case "base":
			$planid = intval($_GET['planid']);
			$strPlan = $new['plan']->find('plan',array(
				'planid'=>$planid,
			));

			if ($strPlan ['addtime'] < strtotime('-7 day') && $TS_USER ['isadmin'] == 0) {
				tsNotice('超过一周，不允许编辑，如需代为编辑请给微澜图书馆公众号后台留言');
			}

			$strPlan['content'] = tsDecode($strPlan['content']);
			$arrGroup = $new['plan']->findAll('group','isplace=2','groupid asc');
			if($strPlan['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
				tsNotice('非法操作！');
			}
			
			$title = '编辑方案信息';
			include template("edit_base");
			break;
		
		case "basedo":

			$planid = intval($_POST['planid']);
            $strPlan = $new['plan']->find('plan',array(
                'planid'=>$planid,
            ));
            if($strPlan['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }

			$title = trim($_POST['title']);
			$typeid = intval($_POST['typeid']);
			$content = tsClean($_POST['content']);

			$img_path = getTextPhotos(tsDecode($content));
			if($img_path==''){
				tsNotice('请至少插入一张图片作为封面！');
			};
			$count = count($img_path);
			$img_path = $img_path[$count-1];
			//$url=preg_replace('#/$#','',SITE_URL);
			//$img_path = $url.$img_path;



			if($title == '' || $content == ''){
				tsNotice('标题和内容不能为空');
			}

			//更新数据
			$new['plan']->update('plan',array(
				'planid'=>$planid,
			),array(
				'title'	=> $title,
				'typeid' => $typeid,
				'content' => $content,
				'isaudit'=>$isaudit,
			));
			
			//上传
			$arrUpload = tsUploadPhotoUrl($img_path,$planid,'plan',array('jpg','gif','png'));
			
			if($arrUpload){

				$new['plan']->update('plan',array(
					'planid'=>$planid,
				),array(
					'path'=>$arrUpload['path'],
					'photo'=>$arrUpload['url'],
				));

                //删除缓存图片
				tsDimg ( $arrUpload ['url'], 'plan', '100', '', $arrUpload ['path'] );
				tsDimg ( $arrUpload ['url'], 'plan', '200', '', $arrUpload ['path'] );
                tsDimg ( $arrUpload ['url'], 'plan', '300', '', $arrUpload ['path'] );
                tsDimg ( $arrUpload ['url'], 'plan', '500', '', $arrUpload ['path'] );

			}
			
			header("Location: ".tsUrl('plan','show',array('id'=>$planid)));
		
			break;
			
		case "join":
			
			$planid = intval($_GET['planid']);
			$strPlan = $new['plan']->find('plan',array(
				'planid'=>$planid,
			));

            if($strPlan['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }
			
			$arrUser = $new['plan']->findAll('plan_users',array(
				'planid'=>$planid,
			),'addtime desc');

			foreach($arrUser as $k => $item){
				$strUser = aac('user')->find('user_info',array('userid'=>$item['userid']));
				$arrUser[$k]['username']=$strUser['username'];
				$arrUser[$k]['phone']=$strUser['phone'];
			}

			
			$title = '申领信息';
			include template('edit_join');
			
			break;

	}